package etilan.Client.Connectors;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import etilan.Client.MainActivity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

public class wifiConn implements Conn{
	
	private int port;
	private String ip;
	private Socket s;
	private OutputStream os;
	private InputStream is;
	private InputStreamReader isr;
	private MainActivity ap;
	private boolean stopping = false;

	public wifiConn(int port1, String ip1){
		port=port1;
		ip=ip1;
	}
	
	public final synchronized void changeParentWindow(MainActivity a){
		ap = a;
		if(ap != null)
			if(s != null)
				if(s.isConnected())
					setConnectedStatus(true, s.getInetAddress().getHostAddress());
	}
	
	private void setConnectedStatus(boolean activated,String ip){
		Message msg = new Message();
		Bundle b = new Bundle();
		b.putString("ip", ip);
		msg.arg1 = activated ? 1 : 0;
		msg.setData(b);
		ap.isConnectedCallback.sendMessage(msg);
	}
	
	public final synchronized void kill(){
		Log.d("DroidPad", "DPC: Thread dying...");
		if(!stopping)
		{
			stopping = true;
			if(isr != null)
			{
				try {
					isr.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(is != null)
			{
				try {
					is.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(os != null)
			{
				try {
					os.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if(s != null)
			{
				try {
					s.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		Log.d("DroidPad", "DPC: Thread dead!");
	}
	
	public void connInit() throws IOException{
		if(stopping) return; // Escape method
		try{
			InetAddress add=(Inet4Address)InetAddress.getByName(ip);
			SocketAddress remoteAddr= new InetSocketAddress(add, port);
			if (s==null){
				s=new Socket();
				s.connect(remoteAddr, 0);
			}
		}
		catch(IOException e){e.printStackTrace();s.close();}
		
		if(!stopping)
		{
			s.setTcpNoDelay(true);
			s.setKeepAlive(true);
			os = s.getOutputStream();
			is = s.getInputStream();
			isr = new InputStreamReader(is);
			setConnectedStatus(true, s.getInetAddress().getHostAddress());
		}
	}
	
	public void write(byte[] data) throws IOException{
		if (!stopping){
			os.write(data);
			os.flush();
		}
	}
	
	public char[] read() throws IOException{
		if(isr.ready() && !stopping){
			char[] ch = new char[1024];
			isr.read(ch);
			String st = new String(ch);
			if(st.startsWith("<STOP>")){
				setConnectedStatus(false,"0.0.0.0");
			}
			return ch;
		}
		return null;
	}
	
	public boolean isStopping(){
		return stopping;
	}
	
	public boolean socketOn(){
		return (s!=null);
	}
}